<template>
  <div class="user-profile-page">
    <van-nav-bar
      left-arrow
      title="个人信息"
      @click-left="$router.back()"
    ></van-nav-bar>
    <van-cell-group>
      <!-- 图片上传组件 -->
      <van-uploader :before-read="beforeRead" :after-read="afterRead">
        <van-cell title="头像" center is-link>
          <van-image
            slot="default"
            fit="cover"
            round
            :src="userInfo.photo"
          ></van-image>
        </van-cell>
      </van-uploader>
      <van-cell title="昵称" :value="userInfo.name" @click="newName" />
      <van-cell title="简介" :value="userInfo.intro" />
    </van-cell-group>
    <van-cell-group style="margin-top:12px">
      <van-cell
        is-link
        title="性别"
        :value="{ 0: '男', 1: '女', 2: '保密' }[userInfo.gender]"
        @click="genderClick"
      ></van-cell>
      <van-cell
        is-link
        title=" 生日"
        :value="userInfo.birthday | formatTime"
        @click="birthdayShow"
      ></van-cell>
    </van-cell-group>
    <div class="logout" @click="logout">
      <span>退出登录</span>
    </div>
    <!-- 性别选择组件 -->
    <van-action-sheet
      v-model="genderShow"
      cancel-text="取消"
      :actions="actions"
      @cancel="genderShow = false"
      @select="genderSelect"
    ></van-action-sheet>

    <!-- 生日选择组件 -->
    <van-popup v-model="showTime" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        title="选择年月日"
        type="date"
        :min-date="minDate"
        :max-date="maxDate"
        confirm-button-text
        cancel-button-text
        @cancel="showTime = false"
        @confirm="newBirthday"
      />
    </van-popup>
    <Edit ref="EditUser"></Edit>
  </div>
</template>
<script>
import Edit from './edit.vue'
import { mapState, mapMutations } from 'vuex'
import { userProfilePatch, userPhotoPatch } from '@/api/login.js'
// userPhotoPatch
import dayjs from 'dayjs'
export default {
  components: {
    Edit
  },
  data () {
    return {
      currentDate: new Date(),
      minDate: new Date('1000-01-01'),
      maxDate: new Date(),
      showTime: false,
      genderShow: false,
      actions: [
        {
          name: '男',
          value: 0
        },
        {
          name: '女',
          value: 1
        }
      ]
    }
  },
  filters: {
    formatTime (time) {
      return dayjs(time).format('YYYY-MM-DD')
    }
  },
  computed: {
    ...mapState('login', ['userInfo'])
  },
  created () {
    this.$store.dispatch('login/getUserInfo')
  },
  methods: {
    ...mapMutations('login', ['setToken', 'setUserGender']),
    logout () {
      this.$dialog
        .confirm({
          title: '确认是否退出登录'
        })
        .then(() => {
          this.$router.push('/')
          this.setToken('')
          this.$toast.success('退出成功')
        })
    },
    // 性别点击
    genderClick () {
      this.genderShow = true
      console.log('159', this.userInfo)
      this.actions[this.userInfo.gender].disabled = true
      this.actions[1 - this.userInfo.gender].disabled = false
    },
    // 性别选项点击
    async genderSelect (item) {
      // await userProfilePatch({ gender: item.value })
      // this.setUserGender({ key: 'gender', value: item.value })
      this.editUserInfo({ key: 'gender', value: item.value })
      console.log(item)
      this.genderShow = false
    },

    // 显示选择生日
    birthdayShow () {
      this.showTime = true
      this.currentDate = new Date(this.userInfo.birthday)
    },
    // 修改生日
    async newBirthday (value) {
      // await userProfilePatch({ birthday: value })
      // this.setUserGender({ key: 'birthday', value: value })
      this.editUserInfo({ key: 'birthday', value })
      this.showTime = false
    },
    // 封装生日 性别修改
    async editUserInfo (obj) {
      await userProfilePatch({ [obj.key]: obj.value })
      this.setUserGender({ key: obj.key, value: obj.value })
      this.$toast.success('修改成功')
    },
    // 图片文件读取完成后的回调函数
    beforeRead (file) {
      console.log('file', file)
      const isImg = file.type === 'image/png' || 'image/jpeg'
      const limit1m = file.size / 1024 / 1024 < 1
      if (!isImg) {
        this.$toast.fail('仅支持png/jpeg格式图片')
      }
      if (!limit1m) {
        this.$toast.fail('图片大小不能超过1M')
      }
      return isImg && limit1m
    },
    async afterRead (res) {
      // console.log('444', res)
      const _formdata = new FormData()
      _formdata.append('photo', res.file)
      const response = await userPhotoPatch(_formdata)
      console.log('response', response)
      this.setUserGender({ key: 'photo', value: response.data.photo })
      this.$toast.success('头像修改成功')
    },
    newName () {
      this.$refs.EditUser.editShow = true
      this.$refs.EditUser.inputValue = this.userInfo.name
    }
  }
}
</script>

<style lang="less" scoped>
.user-profile-page {
  background: #f8f8f8;
  .van-image {
    display: block;
    float: right;
    width: 30px;
    height: 30px;
  }
  .van-cell__title {
    width: 50px;
    flex: none;
  }
}
.logout {
  text-align: center;
  position: absolute;
  left: 0;
  bottom: 30px;
  width: 100%;
  color: @geek-color;
}
.van-uploader {
  width: 100%;
}
::v-deep .van-uploader__input-wrapper {
  width: 100%;
}
// ::v-deep .van-cell .van-cell--clickable {
//   display: flex;
//   justify-content: space-between;
// }
</style>
